// 博客相关的方法
const { execSQL } = require('../db/mysql');

// 获取博客列表
const getBlogsList = (author, keyword) => {
        let sql = `select * from blogs where 1=1 `;

        if (author) {
            sql += `and author='${author}' `;
        }

        if (keyword) {
            sql += `and id= ${keyword}`;
        }

        console.log('sql', sql);

        return execSQL(sql);

    }
    //分页 获取博客列表
const getBlogsListPage = (start, pagesize) => {
    let sql = 'select * from blogs limit ' + start + ' , ' + pagesize;


    return execSQL(sql);

}

// 新增博客
const createNewBlog = (blogData) => {
        // blogData title content author createdAt
        const title = blogData.title;
        const content = blogData.content;
        const author = blogData.author;
        const createdAt = Date.now();
        // console.log(blogData)
        const sql = `
    insert into blogs (title, content, author, createdAt) values ('${title}', '${content}', '${author}', ${createdAt});
  `

        return execSQL(sql).then(insertedResult => {
            console.log('insertedResult', insertedResult);
            return {
                id: insertedResult.insertId
            }
        });
    }
    // 编辑博客
const editBlog = (blogData) => {
    // blogData title content author createdAt
    const title = blogData.title;
    const id = blogData.id;
    const content = blogData.content;
    const sql = `update blogs set title='${title}', content='${content}' where id=${id}; `

    return execSQL(sql).then(insertedResult => {
        console.log('insertedResult', insertedResult);
        return {
            id: insertedResult.insertId
        }
    });
}

// 删除博客
const deleteBlog = (id) => {
    const sql = `delete from blogs where id=${id} `;
    return execSQL(sql).then(insertedResult => {
        console.log('insertedResult', insertedResult);
        return {
            id: insertedResult.insertId
        }
    });
}


// 创建新的用户
const createUser = (username, password) => {
    // const author = author1;
    // const password = password1;
    const sql = `
            insert into user (username, password) values ('${username}', '${password}');
          `

    return execSQL(sql).then(insertedResult => {
        console.log('insertedResult', insertedResult);
        return {
            id: insertedResult.insertId
        }
    });
}

//
// 登录查找用户名是否存在
const findUsername = (username) => {
    // const author = author1;
    // const password = password1;
    const sql = `select * from user where username='${username}' `

    return execSQL(sql).then(insertedResult => {

        // console.log('insertedResult是', insertedResult);

        return {
            user: insertedResult
        }
    });
}

// 登录查找用户名是否存在
const findUserId = (id) => {
        // const author = author1;
        // const password = password1;
        const sql = `select * from user where id='${id}' `

        return execSQL(sql).then(insertedResult => {

            // console.log('insertedResult是', insertedResult);

            return {
                user: insertedResult
            }
        });
    }
    // 删除博客


module.exports = {
    getBlogsList,
    createNewBlog,
    createUser,
    findUsername,
    findUserId,
    getBlogsListPage,
    editBlog,
    deleteBlog,
}